//
//  DeviceModel.m
//  Fantasia
//
//  Created by kongkong on 2016/11/6.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "DeviceInfoModel.h"
#import "DataFilter.h"

@interface DeviceInfoModel ()
@property(nonatomic, strong) NSMutableDictionary *data;
@property(nonatomic, strong) NSArray *sortedName;

@property(nonatomic, strong) NSMutableDictionary *activeNameKeys;
@property(nonatomic, strong) NSArray *hiddenKeys;
@end

@implementation DeviceInfoModel

+ (DeviceInfoModel *)api:(NSString *)api action:(NSString *)action {
    DeviceInfoModel *model = [[[self class] alloc] init];
    model.api = api;
    model.action = action;

    return model;
}

- (NSDictionary *)nameKey {
    if (!_nameKey) {
        _nameKey = @{@"Name": @"设备名称",
                @"Host": @"设备主机名称",
                @"OSCount": @"设备系统数",
                @"Status": @"设备状态",
                @"LockStatus": @"锁定状态",
                @"AccountBalance": @"余额",
                @"TotalDeposit": @"总产量",
        };
    }
    return _nameKey;
}

- (NSMutableDictionary *)activeNameKeys {
    if (!_activeNameKeys) {
        _activeNameKeys = [self.nameKey mutableCopy];
    }
    return _activeNameKeys;
}


- (NSArray *)sortedName {
    if (!_sortedName) {
        NSArray *keys = self.nameKey.allKeys;
        _sortedName = [[keys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
            return [self.nameKey[obj2] compare:self.nameKey[obj1]];
        }] copy];
    }
    return _sortedName;
}

- (void)reSrotNameKey {
    NSArray *keys = self.activeNameKeys.allKeys;
    _sortedName = [[keys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
        return [self.activeNameKeys[obj2] compare:self.activeNameKeys[obj1]];
    }] copy];
}

- (NSString *)keyWithIndex:(NSIndexPath *)index {
    if (self.sortedName.count <= index.row) {
        return nil;
    }

    return self.sortedName[index.row];
}

- (NSString *)titleWithIndex:(NSIndexPath *)index {
    return self.nameKey[[self keyWithIndex:index]];
}

- (NSString *)detailWithIndex:(NSIndexPath *)index {
    NSString *key = [self keyWithIndex:index];
    if (EMPTY_STR(key)) {
        return nil;
    }

    id value = self.data[key];
    if ([value isKindOfClass:[DataModel class]]) {
        DataModel *model = (DataModel *) value;
        switch (model.type) {
            case DataType_String:
                return model.textValue;
                break;
            case DataType_BoolLogo:
                //return model.boolLogo;
                break;
        }

    }
    return nil;
}

- (DataModel *)dataModelWithIndex:(NSIndexPath *)index {
    NSString* key = [self keyWithIndex:index];
    DataModel* data = [self.data valueForKey:key];
    if (!data) {
        data = [[DataModel alloc]init];
        data.title = self.nameKey[[self keyWithIndex:index]];
        self.data[key] = data;
    }
    return data;
}

- (void)relaodData:(void (^)())complete {
    __weak typeof(self) weakSelf = self;
    NSString *deviceId = [[FantasiaStorage instance] memoryValueForKey:@"currentDeviceID"];
    [[[KKRequestEngine api:self.api action:self.action]
            parameter:@{@"deviceID": deviceId}]
            async:^(KKResponse *_Nullable response, id _Nullable result) {
                __strong typeof(weakSelf) strongSelf = weakSelf;
                strongSelf.data = [response.originResult mutableCopy];

                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    strongSelf.hiddenKeys = [DataFilter filterData:strongSelf.data withGroup:[strongSelf group]];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (complete) {
                            complete();
                        }
                    });
                });

            }];
}

- (void)setHiddenKeys:(NSArray *)hiddenKeys {
    _hiddenKeys = [hiddenKeys copy];

    [_hiddenKeys enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        [self.activeNameKeys removeObjectForKey:obj];
    }];
    [self reSrotNameKey];
    [self.data enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, DataModel* obj, BOOL * _Nonnull stop) {
        obj.title = self.nameKey[obj.title];
    }];
}

- (NSUInteger)count {
    return self.activeNameKeys.count;
}

- (FilterGroup)group {
    return DataGroup_DeviceInfo;
}

@end
